#include <glib.h>

static void _ptr_array_foreach_print_func(gpointer data, gpointer user_data)
{
    g_print("%s: %s(addr:%p) \n", (gchar *)user_data, (gchar *)data, data);
}

static void _ptr_array_free_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

gint main(gint argc, gchar **argv)
{
    GPtrArray *parr = NULL;
    gboolean ret = FALSE;
    gpointer p = NULL;
    gchar str0[8] = {0};
    gchar str1[8] = {0};
    gchar str2[8] = {0};
    gchar str3[8] = {0};
    gchar str4[8] = {0};
    gchar str5[8] = {0};
    gchar str6[8] = {0};
    gchar *str[8] = {NULL};
    
    g_strlcpy(str0, "one", 8);
    g_strlcpy(str1, "two", 8);
    g_strlcpy(str2, "two", 8);
    g_strlcpy(str3, "three", 8);
    g_strlcpy(str4, "four", 8);
    g_strlcpy(str5, "five", 8);
    g_strlcpy(str6, "six", 8);

    str[0] = g_strdup(str0);
    str[1] = g_strdup(str1);
    str[2] = g_strdup(str2);
    str[3] = g_strdup(str3);
    str[4] = g_strdup(str4);
    str[5] = g_strdup(str5);
    str[6] = g_strdup(str6);

    g_print("str1:%s(addr:%p) | str[1]:%s(addr:%p) \n", str1, str1, str[1], str[1]);
    g_print("str3:%s(addr:%p) | str[3]:%s(addr:%p) \n", str3, str3, str[3], str[3]);

    parr = g_ptr_array_sized_new(10);
    
    g_ptr_array_add(parr, (gpointer)str[0]);
    g_ptr_array_add(parr, (gpointer)str[1]);
    g_ptr_array_add(parr, (gpointer)str[2]);
    g_ptr_array_add(parr, (gpointer)str[3]);
    g_ptr_array_add(parr, (gpointer)str[4]);
    g_ptr_array_add(parr, (gpointer)str[5]);
    g_ptr_array_add(parr, (gpointer)str[6]);

    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "ori");

    g_ptr_array_set_free_func(parr, _ptr_array_free_func);

    ret = g_ptr_array_remove(parr, str[1]); // Caution: Not str1, Not "two"!
    g_print("remove(two) ret: %s \n", ret ? "TRUE" : "FALSE");
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove(two)");

    p = g_ptr_array_remove_index(parr, 1);
    g_print("remove_index(1) p:%p \n", p);
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_index(1)");

    ret = g_ptr_array_remove_fast(parr, str[3]);  // Caution: Not str3, Not "three"!
    g_print("remove_fast(three) ret: %s \n", ret ? "TRUE" : "FALSE");
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_fast(three)");

    p = g_ptr_array_remove_index_fast(parr, 2);
    g_print("remove_index_fast(2) p:%p \n", p);
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_index_fast(2)");

    g_ptr_array_remove_range(parr, 0, 2);
    g_ptr_array_foreach(parr, _ptr_array_foreach_print_func, "remove_range(0,2)");

    g_ptr_array_free(parr, TRUE);

    return 0;
}